package io.renren.modules.project.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.renren.modules.oss.cloud.OSSFactory;
import io.renren.modules.project.mapper.ProjectMapper;
import io.renren.modules.project.model.ProjectBo;
import io.renren.modules.project.model.ProjectDo;
import io.renren.modules.project.model.ProjectVo;
import io.renren.modules.project.service.ProjectService;
import io.renren.modules.sys.service.SysConfigService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("projectService")
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private SysConfigService sysConfigService;

    @Override
    public PageInfo<ProjectVo> queryPage(Integer pageNum, Integer pageSize, String projectName, Date createTime, Date endTime, Integer projectStatus, Integer createUserId) {
        if (ObjectUtils.isNotEmpty(pageNum) && ObjectUtils.isNotEmpty(pageSize)) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<ProjectVo> projectVos = projectMapper.queryPageList(projectName, createTime, endTime, projectStatus,createUserId);
        PageInfo<ProjectVo> goodsVoPageInfo = new PageInfo<>(projectVos);
        return goodsVoPageInfo;
    }


    @Override
    public ProjectDo getById(Long id) {
        ProjectDo byId = projectMapper.getById(id);
        return byId;
    }

    @Override
    public Integer save(ProjectBo projectBo) {
        projectBo.setFilePath(projectBo.getFilePath().replace("http://qked74qd9.hb-bkt.clouddn.com/", ""));
        ProjectDo projectDo = new ProjectDo();
        BeanUtils.copyProperties(projectBo, projectDo);
        return projectMapper.save(projectDo);
    }

    @Override
    public Integer updateById(ProjectBo projectBo) {
        projectBo.setFilePath(projectBo.getFilePath().replace("http://qked74qd9.hb-bkt.clouddn.com/", ""));
        ProjectDo projectDo = new ProjectDo();
        BeanUtils.copyProperties(projectBo, projectDo);
        return projectMapper.updateById(projectDo);
    }

    @Override
    public Integer removeById(Long[] ids) {
        return projectMapper.removeById(ids);
    }

    @Override
    public Map<String, String> getDownLoadPath(Long id) {
        Map<String, String> res = new HashMap<>();
        String downloadSuffix = "";
        ProjectDo projectDo = projectMapper.getById(id);
        String filePath = projectDo.getFilePath();
        if (StringUtils.isNotBlank(filePath)) {
            try {
                downloadSuffix = OSSFactory.build().downloadSuffix(filePath);
                res.put("originalPath", downloadSuffix);
                String replace = downloadSuffix.replace("http://qked74qd9.hb-bkt.clouddn.com/", "");
                res.put("name", replace);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return res;
    }
}